﻿Imports System.IO

Public Class Frm_Avoc_Bure
    Dim bol As Boolean = True
    Public avo As Avocat_bureau
    Public Sub vider()
        Partager.list_hatif.Clear()
        txt_cin.Clear()
        txt_3a2ili.Clear()
        txt_chakhssi.Clear()
        txt_nom.Clear()
        txt_prenom.Clear()
        Check_nachit.Checked = False
        If comb_ville.Items.Count > 0 Then
            comb_ville.SelectedIndex = 0
        End If
        txt_adresse.Clear()
        If comb_hala_ijtim.Items.Count > 0 Then
            comb_hala_ijtim.SelectedIndex = 0
        End If
        If comb_wadifa.Items.Count > 0 Then
            comb_wadifa.SelectedIndex = 0
        End If
        txt_num_mihani.Clear()
        txt_molahadat.Clear()
        If Comb_hay2a.Items.Count > 0 Then
            Comb_hay2a.SelectedIndex = 0
        End If
        txt_salaire.Clear()
        txt_mail.Clear()
        Pic_avoc.Image = My.Resources.photo
        txt_3a2ili.Select()
        Frm_Tele_Bure.List_tele.Items.Clear()

        If List_tous.SelectedIndices.Count > 0 Then
            For i As Integer = 0 To List_tous.SelectedIndices.Count - 1
                List_tous.Items(List_tous.SelectedIndices(i)).Selected = False
            Next
        End If
    End Sub

    Dim loc As String = "photo.jpg"
    Private Sub Btn_parc_halaijtima_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Btn_parc_wadifa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_wadifa.Click
        Partager.wadifa = "avocat"
        Me.Enabled = False
        Frm_Avoc_Bure_wadifa.txt_wadifa.Select()
        Frm_Avoc_Bure_wadifa.Show()
    End Sub

    Private Sub Btn_parc_hay2a_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_hay2a.Click
        Me.Enabled = False
        Frm_Avoc_Bure_hay2a.txt_hay2a.Select()
        Frm_Avoc_Bure_hay2a.Show()
    End Sub

    Private Sub Btn_ajou_tele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele.Click
        Partager.typePhone = "Tel"
        Partager.isTelClicked = True
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()

        '------
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_hatif.Clear()
                For Each numhone As Phone In avo.list_phone
                    Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_parc_ville_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ville.Click
        Me.Enabled = False
        Frm_Avoc_Bure_ville.txt_ville.Select()
        Frm_Avoc_Bure_ville.Show()
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Partager.rech = False
        Frm_Rech_Avoc_Clients.Show()
    End Sub

    Private Sub Frm_Avoc_Bure_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_3a2ili.Select()
        comb_hala_ijtim.SelectedIndex = 0
        Try
            Partager.moi.remplir_List_avocat_bureau()
            List_tous.Items.Clear()
            For Each khib As Avocat_bureau In Partager.moi.List_avocat_bureau
                If khib.type <> "صاحب المكتب" Then
                    List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Cin, khib.Nom_a, khib.Prenom_a, khib.Num_prof, khib.Type_avocat, khib.Ville, khib.Date_employement}))
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Partager.com_ville(comb_ville, "Comb_Ville", "Ville")
        Partager.com_ville(comb_wadifa, "comb_wadifa", "wadifa")
        Partager.com_ville(Comb_hay2a, "Comb_hay2a", "hay2a")

    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة محام جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If txt_mail.Text <> "" Then
                    bol = Partager.regEmail(txt_mail.Text)
                    If (bol = False) Then
                        txt_mail.Text = "البريد الإلكتروني خاطئ"
                        txt_mail.ForeColor = Color.Red
                        txt_mail.TextAlign = HorizontalAlignment.Right
                    End If
                End If

                If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "" And bol = True) Then


                    For Each av As Avocat_bureau In Partager.moi.List_avocat_bureau
                        If txt_cin.Text <> "" And av.Cin = txt_cin.Text Then
                            MsgBox("رقم البطاقة استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                            Exit Sub
                        End If

                        If txt_num_mihani.Text <> "" Then
                            If IsNumeric(txt_num_mihani.Text) Then
                                If av.Num_prof = txt_num_mihani.Text Then
                                    MsgBox("الرقم المهني استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                                    Exit Sub
                                End If
                            Else
                                MsgBox("خطأ في الرقم المهني", MsgBoxStyle.Critical, "خطأ")
                                Exit Sub
                            End If
                        End If
                    Next


                    If txt_num_mihani.Text <> "" Then
                        If Not IsNumeric(txt_num_mihani.Text) Then
                            MsgBox("خطأ في الرقم المهني", MsgBoxStyle.Critical, "خطأ")
                            Exit Sub
                        End If
                    End If


                    If MsgBox("هل تريد فعلا إضافة هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة محامين المكتب") = MsgBoxResult.Yes Then
                        Try
                            Partager.ControleNull(Me)
                            Dim avovat_bur As New Avocat_bureau(1, txt_cin.Text, txt_nom.Text, txt_prenom.Text, txt_3a2ili.Text, txt_chakhssi.Text, Date_nissance.Value.Date, Date_tawdif.Value.Date, comb_ville.Text, comb_hala_ijtim.Text, txt_num_mihani.Text, comb_wadifa.Text, Comb_hay2a.Text, txt_salaire.Text, txt_mail.Text, txt_adresse.Text, Partager.image(Pic_avoc), txt_molahadat.Text, Check_nachit.Checked, "محامي المكتب", False)
                            'ajouter dans la base et remplissage de la listAvocat 
                            Partager.moi.Ajouter_avocat_bureau(avovat_bur)
                            'l'objet en cours
                            Dim lastItem As Integer = Partager.moi.List_avocat_bureau.Count - 1
                            avovat_bur = CType(Partager.moi.List_avocat_bureau.Item(lastItem), Avocat_bureau)

                            If Partager.list_hatif.Count > 0 Then
                                avovat_bur.suprimmer_phone(avo.Id, "Tel")
                                For Each d As Phone In Partager.list_hatif
                                    avovat_bur.Ajouter_phone(d)
                                Next
                            End If

                            List_tous.Items.Clear()
                            For Each khib As Avocat_bureau In Partager.moi.List_avocat_bureau
                                If khib.type <> "صاحب المكتب" Then
                                    List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Cin, khib.Nom_a, khib.Prenom_a, khib.Num_prof, khib.Type_avocat, khib.Ville, khib.Date_employement}))
                                End If
                            Next
                            MsgBox("تمت إضافة المحامي بنجاح", MsgBoxStyle.Information, "نجاح")
                            vider()
                        Catch ex As Exception
                            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                            Partager.close()
                        End Try
                    End If


                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
                End If
        Catch ex As Exception

            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub Btn_parc_photo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_photo.Click
        With OpenF
            .FileName = ""
            .Title = "Ovrir la Photo"
            .RestoreDirectory = True
            .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            .Filter = "JPEG Files (*.JPG)|*.JPG"
            .FilterIndex = 1
        End With
        If OpenF.ShowDialog = Windows.Forms.DialogResult.OK Then
            Pic_avoc.Image = Image.FromFile(OpenF.FileName)
            loc = OpenF.FileName
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged

        If List_tous.SelectedItems.Count > 0 Then
            Try
                Partager.close()
                Dim i As Integer = 0
                Dim id As Integer = CType(List_tous.SelectedItems(0).Text, Integer)

                Dim j As Integer = 0
                While i < List_tous.SelectedIndices.Count
                    avo = Partager.moi.List_avocat_bureau(j)
                    If avo.Id = id Then
                        i += 1
                    End If
                    j += 1
                End While

                txt_3a2ili.Text = avo.Nom_a
                txt_cin.Text = avo.Cin
                txt_nom.Text = avo.Nom
                txt_prenom.Text = avo.Prenom
                txt_3a2ili.Text = avo.Nom_a
                txt_chakhssi.Text = avo.Prenom_a
                Date_nissance.Value = avo.Date_naiss.ToString
                Date_tawdif.Value = avo.Date_employement.ToString
                comb_ville.Text = avo.Ville
                comb_hala_ijtim.Text = avo.Situation_social
                txt_num_mihani.Text = avo.Num_prof
                comb_wadifa.Text = avo.Type_avocat
                Comb_hay2a.Text = avo.Autorite
                If avo.Salaire = 0 Then
                    txt_salaire.Text = ""
                Else
                    txt_salaire.Text = avo.Salaire
                End If
                txt_mail.Text = avo.Email
                txt_adresse.Text = avo.Adress
                Dim bytBLOBData() As Byte = avo.Photo
                Dim stmBLOBData As New MemoryStream(bytBLOBData)
                Pic_avoc.Image = Image.FromStream(stmBLOBData)

                txt_molahadat.Text = avo.Remarque

                If avo.Actif Then
                    Check_nachit.Checked = True
                Else
                    Check_nachit.Checked = False
                End If
                avo.remplir_List_list_phone()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Partager.list_hatif.Add(numhone)
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

        If List_tous.SelectedIndices.Count <= 0 Then
            Partager.vider(Me)
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف محامين المكتب") = MsgBoxResult.Yes Then
                Try
                    Partager.moi.suprimmer_avocat_bureau(avo.Id)
                    'khib.suprimmer_phone(khib.Id, "Tel")
                    List_tous.Items.Clear()
                    For Each khib As Avocat_bureau In Partager.moi.List_avocat_bureau
                        If khib.type <> "صاحب المكتب" Then
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Cin, khib.Nom_a, khib.Prenom_a, khib.Num_prof, khib.Type_avocat, khib.Ville, khib.Date_employement}))
                        End If
                    Next
                    vider()
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر محام من القائمة", MsgBoxStyle.Information, "حذف")
        End If
    End Sub



    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        Try
            If List_tous.SelectedIndices.Count > 0 Then


                If txt_mail.Text <> "" Then
                    bol = Partager.regEmail(txt_mail.Text)
                    If (bol = False) Then
                        txt_mail.Text = "البريد الإلكتروني خاطئ"
                        txt_mail.ForeColor = Color.Red
                        txt_mail.TextAlign = HorizontalAlignment.Right
                    End If
                End If

                If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "" And bol = True) Then

                    For Each av As Avocat_bureau In Partager.moi.List_avocat_bureau
                        If avo.Id <> av.Id Then

                            If txt_cin.Text <> "" And av.Cin = txt_cin.Text Then
                                MsgBox("رقم البطاقة استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                                Exit Sub
                            End If

                            If txt_num_mihani.Text <> "" Then
                                If IsNumeric(txt_num_mihani.Text) Then
                                    If av.Num_prof = txt_num_mihani.Text Then
                                        MsgBox("الرقم المهني استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                                        Exit Sub
                                    End If
                                Else
                                    MsgBox("خطأ في الرقم المهني", MsgBoxStyle.Critical, "خطأ")
                                    Exit Sub
                                End If
                            End If

                        End If
                    Next


                    If txt_num_mihani.Text <> "" Then
                        If Not IsNumeric(txt_num_mihani.Text) Then
                            MsgBox("خطأ في الرقم المهني", MsgBoxStyle.Critical, "خطأ")
                            Exit Sub
                        End If
                    End If

                    If MsgBox("هل تريد فعلا تعديل معلومات هذا المحامي ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل محامين المكتب") = MsgBoxResult.Yes Then

                        Partager.ControleNull(Me)
                        Dim avova As New Avocat_bureau(1, txt_cin.Text, txt_nom.Text, txt_prenom.Text, txt_3a2ili.Text, txt_chakhssi.Text, Date_nissance.Value.Date, Date_tawdif.Value.Date, comb_ville.Text, comb_hala_ijtim.Text, txt_num_mihani.Text, comb_wadifa.Text, Comb_hay2a.Text, txt_salaire.Text, txt_mail.Text, txt_adresse.Text, Partager.image(Pic_avoc), txt_molahadat.Text, Check_nachit.Checked, "محامي المكتب", False)
                        Partager.moi.modifier_avocat_bureau(avo.Id, avova)
                        avova.Id = avo.Id
                        If Partager.list_hatif.Count > 0 Or Partager.isTelClicked Then
                            avova.suprimmer_phone(avo.Id, "Tel")
                            For Each d As Phone In Partager.list_hatif
                                avova.Ajouter_phone(d)
                            Next
                        End If
                        List_tous.Items.Clear()
                        For Each khib As Avocat_bureau In Partager.moi.List_avocat_bureau
                            If khib.type <> "صاحب المكتب" Then
                                List_tous.Items.Add(New ListViewItem(New String() {khib.Id, khib.Cin, khib.Nom_a, khib.Prenom_a, khib.Num_prof, khib.Type_avocat, khib.Ville, khib.Date_employement}))
                            End If
                        Next
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        vider()

                    End If

                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            Else
                MsgBox("اختر محام من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Partager.close()
        End Try
    End Sub

    Private Sub txt_mail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_mail.Click
        If bol = False Then
            txt_mail.Text = ""
        End If
        bol = True
    End Sub

    Private Sub txt_mail_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mail.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_mail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mail.TextChanged
        txt_mail.ForeColor = Color.Black
        txt_mail.TextAlign = HorizontalAlignment.Left
    End Sub

    Private Sub txt_cin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_cin.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        vider()
    End Sub

    Private Sub Btn_photo_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_photo_supp.Click
        Pic_avoc.Image = My.Resources.photo
    End Sub

    Private Sub comb_ville_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles comb_ville.KeyPress
        e.Handled = True
    End Sub

    Private Sub comb_hala_ijtim_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles comb_hala_ijtim.KeyPress
        e.Handled = True
    End Sub

    Private Sub comb_wadifa_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles comb_wadifa.KeyPress
        e.Handled = True
    End Sub

    Private Sub Comb_hay2a_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Comb_hay2a.KeyPress
        e.Handled = True
    End Sub

    Private Sub txt_tele_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub txt_num_mihani_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_num_mihani.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txt_salaire_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_salaire.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub
End Class